/*
 *  $TEAM_NAME Space Trader
 */
package presenters;

import models.Player;
import models.Shipyard;
import models.Ship;
import models.FuelTank;

import views.ShipPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.util.Observer;
import java.util.Observable;

/**
 * Ship presenter
 * @author twb
 * @version 1.0
 */
public class ShipPresenter extends Presenter<ShipPanel> 
implements ActionListener, ChangeListener, Observer {
	/**
	 * field
	 */
	private final Player player;

	/**
	 * field
	 */
	private Shipyard shipyard;
    
	/**
	 * field
	 */
	private Ship ship;
	
	/**
	 * creates new ship presenter
	 * @param player
	 * @param view
	 */
	public ShipPresenter(Player player, ShipPanel view) {
        this.shipyard = player.getLocation().getShipyard(); 
        this.ship = player.getShip();
        this.player = player;

        super.setView(view);

        view.addActionListener(this);

        player.addObserver(this);
        ship.addObserver(this);
        shipyard.addObserver(this);
    }

	/**
	 * action performed
	 * @param e
	 */
    @Override
    public void actionPerformed(ActionEvent e) {
        final Object source = e.getSource();

        if (source instanceof JButton) {
            final JButton button = (JButton) source;

            final Ship myShip = player.getShip();
            final Ship newShip = view.getShip();
            int cost;
            if (newShip != null) {
            	 cost = shipyard.getPrice(newShip);
            } else {
            	final FuelTank fuel = myShip.getFuelTank();
            	cost = ((fuel.getMaxFuel() 
            			- fuel.getCurrentFuel()) * myShip.getFUEL_COST());
            }

            if ("Buy".equals(button.getText())) {
                if (cost <= player.getCredits()) {
                    if(player.useCredits(cost) == 0){
                    	System.out.print("");
                    }
                    if (newShip != null){
                    	player.setShip(newShip);
                    	if(!shipyard.canBuy(myShip, newShip)){
                    		System.out.print("");
                    	}
                    }
                    else{
                    	final ShipPanel sp = (ShipPanel) button.getParent();
                    	if(sp.getHealth() != null){
                    		this.player.getShip().getHealth().setValue(
                    				this.player.getShip().getHealth().getMaximum());
                    	} else {
	                    	myShip.getFuelTank().setCurrentFuel(
	                    			myShip.getFuelTank().getMaxFuel());
	                    	if(!shipyard.canBuy(myShip)){
	                    		System.out.print("");
	                    	}
                    	}
                    }
                    player.notifyObservers();
                    ship.notifyObservers();
                    shipyard.notifyObservers();
                } else {
                    JOptionPane.showMessageDialog(
                    		frame, "You don't have enough money for that!");
                    System.out.println("You don't have enough money for that!");
                }
            }
        }
    }

    /**
     * update
     * @param o
     * @param arg
     */
    @Override
    public void update(Observable o, Object arg) {
        if (o.equals(player)) {
            shipyard = player.getLocation().getShipyard();
            ship = player.getShip();
        }

        view.update();
    }
    
    /**
     * state changed
     * @param ce
     */
    @Override
    public void stateChanged(ChangeEvent ce) {
    	System.out.println(ce);
    }
    
    /**
     * to string
     * @return string
     */
    @Override
    public String toString() {
		return "";
	}
}
